package com.vf.admin.client.controller.pay.newAliPay.service;

import com.alibaba.fastjson.JSONObject;
import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradeAppPayRequest;
import com.alipay.api.response.AlipayTradeAppPayResponse;
import com.vf.admin.client.controller.pay.newAliPay.AppAliPayConstants;
import com.vf.admin.client.result.PayInfoResult;
import com.vf.admin.client.service.AppOrderService;
import com.vf.admin.client.service.VipOrderService;
import com.vf.admin.common.Constants;
import com.vf.admin.miaodong.entity.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Date;

import static com.vf.admin.utils.RandomUtil.getOrderIdByUUId;

/**
 * <p>Title : </p>
 * <p>Description : </p>
 * <p>DevelopTools : IDEA</p>
 * <p>DevelopSystem : Windows11</p>
 *
 * @Author : zxw
 * @Date 2023/11/15 15:02
 */
@Service
public class AppAliPayService {

    //钱包订单
    @Autowired
    private AppOrderService appOrderService;
    //vip订单
    @Autowired
    private VipOrderService vipOrderService;


    public String aliPayDiamondTransactions(AppUser user, AppCzMoney czMoney, String mobileSpec) {

        //添加订单记录
        AppOrder appOrder = new AppOrder();
        appOrder.setCreatetime(new Date());
        appOrder.setOrdernum(getOrderIdByUUId());//生成订单号
        appOrder.setCzMoneyId(czMoney.getId());
        appOrder.setMoney(new BigDecimal(czMoney.getMoney()));
        appOrder.setStatus(Constants.dingdan_status_dfk);//默认 1、代付款 2、已付款
        appOrder.setType(Constants.dingdan_zhiFuLeiXing_zfb); //订单支付类型  默认 1、支付宝 2、微信 3、苹果  4、杉德支付宝 5、杉德微信
        appOrder.setUserid(user.getId());//下单人信息
        appOrder.setPic(user.getPic());
        appOrder.setNick(user.getNick());
        appOrder.setPhone(user.getLoginname());
        appOrder.setNum(czMoney.getDiamonds().add(czMoney.getGive()));
        appOrder.setMobileSpec(mobileSpec);
        appOrderService.save(appOrder);
        //***************** 支付宝预处理 **********************
        AlipayTradeAppPayRequest request = new AlipayTradeAppPayRequest();
        request.setNotifyUrl("http://testapi.nhfaqw.com/api/new/aliPayVersion3/diamond/AiliNotifyUrl");
        JSONObject bizContent = new JSONObject();
        bizContent.put("out_trade_no", appOrder.getOrdernum());
        bizContent.put("total_amount", BigDecimal.valueOf(czMoney.getMoney()).setScale(2, BigDecimal.ROUND_HALF_UP).stripTrailingZeros().toPlainString());
        bizContent.put("subject", "钻石充值");
        bizContent.put("product_code", "QUICK_MSECURITY_PAY");
        request.setBizContent(bizContent.toString());
        //返回支付宝订单信息(预处理)
        AlipayClient alipayClient = new DefaultAlipayClient(
                AppAliPayConstants.GATEWAY_URL,
                AppAliPayConstants.APP_ID,
                AppAliPayConstants.PRIVATE_KEY,
                AppAliPayConstants.FORMAT_TYPE,
                AppAliPayConstants.CHARSET_TYPE,
                AppAliPayConstants.PUBLIC_KEY,
                AppAliPayConstants.SIGN_TYPE
        );

        try {
            AlipayTradeAppPayResponse response = alipayClient.sdkExecute(request);
            if (response.isSuccess()) {

                return response.getBody();
            }
        } catch (AlipayApiException e) {
            e.printStackTrace();
        }
        return null;
    }


    public String aliPayVipTransactions(AppUser user, VipPackage vipPackage, String mobileSpec) {
        //添加订单记录
        VipOrder vipOrder = new VipOrder();
        vipOrder.setUserId(user.getId());
        vipOrder.setOrderNo(getOrderIdByUUId());//订单号
        vipOrder.setVipPackageId(vipPackage.getId());
        vipOrder.setStatus(0);  //0待支付 1已支付
        vipOrder.setPayType("alipay");
        vipOrder.setCreateTime(new Date());
        vipOrder.setType(Constants.dingdan_zhiFuLeiXing_zfb); //订单支付类型  默认 1、支付宝 2、微信 3、苹果  4、杉德支付宝 5、杉德微信
        vipOrder.setMobileSpec(mobileSpec);
        vipOrder.setNick(user.getNick());
        vipOrder.setPhone(user.getLoginname());
        vipOrder.setPrice(vipPackage.getPrice());
        vipOrder.setPic(user.getPic());
        vipOrderService.save(vipOrder);
        //***************** 支付宝预处理 **********************
        AlipayTradeAppPayRequest request = new AlipayTradeAppPayRequest();
        request.setNotifyUrl("http://testapi.nhfaqw.com/api/new/aliPayVersion3/diamond/AiliNotifyUrl");
        JSONObject bizContent = new JSONObject();
        bizContent.put("out_trade_no", vipOrder.getOrderNo());
        bizContent.put("total_amount", vipPackage.getPrice().setScale(2, BigDecimal.ROUND_HALF_UP).stripTrailingZeros().toPlainString());
        bizContent.put("subject", "VIP充值");
        bizContent.put("product_code", "QUICK_MSECURITY_PAY");
        request.setBizContent(bizContent.toString());
        //返回支付宝订单信息(预处理)
        AlipayClient alipayClient = new DefaultAlipayClient(
                AppAliPayConstants.GATEWAY_URL,
                AppAliPayConstants.APP_ID,
                AppAliPayConstants.PRIVATE_KEY,
                AppAliPayConstants.FORMAT_TYPE,
                AppAliPayConstants.CHARSET_TYPE,
                AppAliPayConstants.PUBLIC_KEY,
                AppAliPayConstants.SIGN_TYPE
        );

        try {
            AlipayTradeAppPayResponse response = alipayClient.sdkExecute(request);
            if (response.isSuccess()) {
                System.out.println("xxxx:"+response.getBody());

                return response.getBody();
            }
        } catch (AlipayApiException e) {
            e.printStackTrace();
        }
        return null;

    }


}
